class Http {
  constructor(options = {}) {
    this.baseUrl = options['baseUrl']
    this.options = {
      cache: options['cache'] || 'no-cache',
      credentials: options['credentials'] || 'same-origin',
      headers: {
        'content-type': options['content-type'] || 'application/json',
        'Authorization': options['Authorization'] || ''
      },
      mode: options['mode'] || 'cors',
      redirect: options['redirect'] || 'follow',
      referrer: options['referrer'] || 'no-referer'
    }
  }

  async post(url, data) {
    const response = await fetch(this.baseUrl + url, {
      body: JSON.stringify(data),
      method: 'POST',
      ...this.options
    }).catch(err => err);
    if(response.ok) {
      return response.json();
    }
  }

  async get(url) {
    const response = await fetch(this.baseUrl + url, {
      method: 'GET',
      ...this.options
    });
    return response.json();
  }

  async put(url, data) {
    const response = await fetch(this.baseUrl + url, {
      body: JSON.stringify(data),
      method: 'PUT',
      ...this.options
    });
    return response.json();
  }

  async delete(url) {
    const response = await fetch(this.baseUrl + url, {
      method: 'DELETE',
      ...this.options
    });
    return response.json();
  }
}

export default new Http({baseUrl: 'http://localhost:5000'});